﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Utils;

namespace BitPortal.Admin.Language.Module
{
    public partial class LanguageAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                Models.Language lang = new Models.Language
                {
                    LanguageCode = txtCode.Text,
                    Name = txtName.Text,
                    IsEnabled = ckIsEnabled.Checked,
                    FlagImage = FlagImage.TextFieldURL.Text
                };
                if (cbCode.SelectedItem != null && cbCode.SelectedItem.Value != null)
                {
                    lang.LanguageCode = cbCode.SelectedItem.Value;
                }
                if (lang.Save() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Cập nhật ngôn ngữ thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }
        public void BindData(string languageCode = null)
        {
            if (languageCode == null)
            {
                txtName.Text = FlagImage.TextFieldURL.Text = "";
                cbCode.Selectable = true;
                ckIsEnabled.Checked = true;

                Window1.Show();
                return;
            }
            Models.Language lang = Business.LanguageBLL.GetByLanguageCode(languageCode);
            if (lang != null)
            {
                txtCode.Text = lang.LanguageCode;
                cbCode.Selectable = false;
                cbCode.SelectedItem.Value = lang.LanguageCode;
                txtName.Text = lang.Name;
                ckIsEnabled.Checked = lang.IsEnabled;
                FlagImage.TextFieldURL.Text = lang.FlagImage;
                Window1.Show();
            }
        }

    }
}